#!/usr/bin/env python
#encoding=utf-8
def db_DML_execute(connection,sql):
    cursor = connection.cursor()
    try:
        cursor.execute(sql)
        cursor.execute("commit;")
    except Exception as err:
        print(err)
    finally:
        cursor.execute("rollback;")
        cursor.close()

def db_query_execute(connection,sql,istuple):
    cursor = connection.cursor()
    try:
        cursor.execute(sql)
        if istuple=='1':
            return cursor.fetchall()
        else:
            return dictfetchall(cursor)
    except Exception as err:
        print(err)
    finally:
        cursor.close()


def dictfetchall(cursor):
    "将游标返回的结果保存到一个字典对象中"
    desc = cursor.description
    return [dict(zip([col[0] for col in desc],row)) for row in cursor.fetchall()]